package com.houdoudou.customer.buy;

import net.sf.json.JSONObject;

import com.houdoudou.bean.Product;
import com.houdoudou.customer.R;
import com.houdoudou.customer.common.SerializableJSONObject;

import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class ProductAct extends FragmentActivity{

	


	TextView productName;
    ImageButton back;
    JSONObject Current_Product_JSON;
    FragmentManager myFragmentMan ;
    Fragment product_detail;
   
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.act_product);
	//	SerializableJSONObject sjo= (SerializableJSONObject) getIntent().getExtras().getSerializable("productInfo_SJO");
	 //   Current_Product_JSON=sjo.getJSONObject();
		Current_Product_JSON=JSONObject.fromObject(getIntent().getStringExtra("product"));
		myFragmentMan= getSupportFragmentManager();
		productName=(TextView) findViewById(R.id.tv_productname_act_product);
	    productName.setText(Current_Product_JSON.getString(Product.NAME));
		back= (ImageButton) findViewById(R.id.back_act_product);
		back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread () { 
					public void run () { 
					try { 
						
					Instrumentation inst= new Instrumentation(); 
					inst.sendKeyDownUpSync(KeyEvent. KEYCODE_BACK); 
					} catch(Exception e) { 
					e.printStackTrace(); 
					} 
					} 
					}.start();
	
			}
		});
		
		product_detail=new FrgProductDetail();
		Bundle bundle= new Bundle();
		bundle.putString("product", getIntent().getStringExtra("product"));
		product_detail.setArguments(bundle);
		
		 FragmentTransaction  transaction = myFragmentMan.beginTransaction();	      
		 transaction.replace(R.id.rt_container_act_product, product_detail).commit();
	}
	
	

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		//keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0
		 if (keyCode == KeyEvent.KEYCODE_BACK) {
			 
             if(FrgProductDetail.isShowSaved){
           	  FrgProductDetail.hide_lt_saved();   
             return true; 
             }else{
            	 setResult(1, getIntent());
            	 ProductAct.this.finish(); 
             }
             
        }
		return super.onKeyDown(keyCode, event);
	}
}
